package com.leetcode.array_matrix;

/**
 * @author Dennis Li
 * @date 2020/10/15 21:10
 */
public class RemoveElement_27 {
    // 就是快慢指针来做
    public int removeElement(int[] nums, int val) {
        int i = 0, j = nums.length - 1, count = 0;
        while (i <= j) {
            while (j >= 0 && nums[j] == val) {
                count++;
                j--;
            }
            // 保证末尾不是val
            if (j >= i && nums[i] == val) {
                swap(nums, i, j);
            }
            i++;
        }
        return nums.length - count;
    }

    private void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}
